
package com.heimali.happyreader.core.net;

import android.text.TextUtils;

import com.heimali.happyreader.config.ActionCode;
import com.heimali.happyreader.config.ErrorCode;
import com.heimali.happyreader.core.bean.Article;
import com.heimali.happyreader.core.bean.Intro;
import com.heimali.happyreader.core.bean.ManagedRuntimeException;
import com.heimali.happyreader.core.bean.User;
import com.heimali.happyreader.debug.Console;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class JsonParser {
    private static final String TAG = "JsonParse";

    public static ArrayList<Intro> listParser(String str) {
        ArrayList<Intro> intros = new ArrayList<Intro>();
        if (TextUtils.isEmpty(str)) {
            return intros;
        }

        try {
            JSONObject res = new JSONObject(str);
            String errno = res.getString("errno");
            if ("0".equals(errno)) {
                JSONArray array = res.getJSONArray("data");
                Intro item = null;
                for (int i = 0, len = array.length(); i < len; i++) {
                    JSONObject obj = (JSONObject) array.opt(i);
                    item = new Intro();
                    if (obj.has("id")) {
                        item.id = obj.getInt("id");
                    }
                    if (obj.has("time")) {
                        item.time = obj.getString("time");
                    }
                    if (obj.has("title")) {
                        item.title = obj.getString("title");
                    }
                    //                    if (obj.has("intro")) {
                    //                        item.setIntro(obj.getString("intro"));
                    //                    }
                    intros.add(item);
                }
            } else {
                Console.log(TAG, errno, res.getString("errmsg"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return intros;
    }

    public static Article articleParser(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }

        Article article = null;

        try {
            JSONObject res = new JSONObject(str);
            String errno = res.getString("errno");
            if ("0".equals(errno)) {
                JSONObject data = res.getJSONObject("data");
                article = new Article(data.getInt("id"), data.getString("surl"), data.getString("title"),
                        data.getString("content"));
            } else {
                article = new Article();
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Console.log(e.getMessage());
        }

        return article;
    }

    public static User loginInfoParser(String str) {
        try {
            JSONObject obj = new JSONObject(str);
            int suc = obj.getInt("suc");
            if (suc == 1) {
                User user = new User();
                if (obj.has("uid")) {
                    user.setUid(obj.getString("uid"));
                }
                if (obj.has("name")) {
                    user.setUsername(obj.getString("name"));
                }
                if (obj.has("token")) {
                    user.setToken(obj.getString("token"));
                }
                return user;
            }
        } catch (JSONException e) {
            e.printStackTrace();
            throw new ManagedRuntimeException(ErrorCode.JSON_PARSE_ERROR, "login info parse error");
        }
        
        throw new ManagedRuntimeException(ActionCode.LOGIN_PASSWORD_ERROR, "username or password error");
    }

    //    public static UpdateInfo updateInfoParser(String str) {
    //        if (TextUtils.isEmpty(str)) {
    //            return null;
    //        }
    //
    //        UpdateInfo info = null;
    //        try {
    //            JSONObject obj = new JSONObject(str);
    //            info = new UpdateInfo(obj.getInt("minVerCode"), obj.getInt("verCode"), obj.getString("verName"),
    //                    obj.getString("description"), obj.getString("md5"), obj.getString("url"));
    //
    //        } catch (JSONException e) {
    //            e.printStackTrace();
    //            Console.log(e.getMessage());
    //        }
    //
    //        return info;
    //    }
}
